package com.yndsoft.jc.supplier.order;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.UiUtils;

import org.greenrobot.eventbus.EventBus;

public class EntryBox extends SupplierBaseActivity implements View.OnClickListener {

    private EditText boxContent;
    private int position;
    private String oDetId;

    @Override
    public int getContentResId() {
        return R.layout.entry_box;
    }

    public static void start(Context context, int position, String content, String oDetId) {
        Intent intent = new Intent(context, EntryBox.class);
        intent.putExtra(Constants.POSITION, position);
        intent.putExtra(Constants.CONTENT, content);
        intent.putExtra(Constants.ID, oDetId);
        context.startActivity(intent);
    }

    @Override
    public void initView() {

        boxContent = findViewById(R.id.box_content);
        ImageButton boxClose = findViewById(R.id.box_close);
        Button boxPut = findViewById(R.id.box_put);
        boxClose.setOnClickListener(this);
        boxPut.setOnClickListener(this);
        UiUtils.initSoftInputShow(this);
    }

    @Override
    public void initData() {
        position = getIntent().getIntExtra(Constants.POSITION, 0);
        String content = getIntent().getStringExtra(Constants.CONTENT);
        oDetId = getIntent().getStringExtra(Constants.ID);
        boxContent.setText(content != null ? content : "");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.box_close:
                finish();
                break;
            case R.id.box_put:
                String content = boxContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    showHint("请输入检测结果！");
                } else {
                    putCause(content);
                }
                break;
        }
    }

    private void putCause(String content) {
        finish();
        EventModel eventModel = new EventModel();
        eventModel.setInteger(position);
        eventModel.setString(content);
        eventModel.setId(oDetId);
        EventBus.getDefault().post(eventModel);
    }
}
